<template>
  <div class="tttt"></div>
</template>

<script>
// 1、什么是moment.js
// 它是一个简单易用的轻量级Javascript日期处理类库，
//提供了日期格式化、日期解析等功能，且支持在浏览器和NodeJS两种环境中运行
import momont from "moment";
export default {
  name: "test",
  data() {
    return {
      websock: "",
      push: Boolean,
    };
  },
  created() {
    this.push=JSON.parse(sessionStorage.getItem("push"));
    window.addEventListener("setItem", this.savepush,false);
  },
  mounted() {
    this.initWebSocket();
  },
  beforeDestroy(){
     
  },
  watch: {
    push(newVal, oldVal) {
      if (newVal) {
        this.initWebSocket();
      } 
      if(!newVal){
        this.websock.close();
      }
    },
  },
  methods: {
    savepush(){
      this.push = JSON.parse(sessionStorage.getItem("push"));
    },
    initWebSocket() {
      //初始化weosocket
      //ws地址
      let token = sessionStorage.getItem("token");
      const wsuri = `ws://47.106.236.114:8090/websocket/alarm/${token}`;
      this.websock = new WebSocket(wsuri);
      this.websock.onmessage = this.websocketonmessage;
    },
    websocketonmessage(e) {
      //数据接收
      const redata = JSON.parse(e.data);
      this.$store.commit("replacewarn", redata); //把最新告警存储到vuex

      // if (redata["温度告警"]) {
      //   let { id, siteNo, siteName, alarmTime, channel, workNo } = redata[
      //     "温度告警"
      //   ];
      //   if (alarmTime) {
      //     alarmTime = momont(alarmTime).format("YYYY-MM-DD HH:mm:ss");
      //   }
      //   this.$Notification({
      //     alarmId: id,
      //     type: "alarming",
      //     alarmType: "温度告警",
      //     siteName,
      //     siteNo,
      //     alarmTime,
      //     aisleNo: channel,
      //     workNo,
      //   });
      // }
      // if (redata["温度告警关闭"]) {
      //   let { id, siteNo, siteName, alarmTime, channel, workNo } = redata[
      //     "温度告警关闭"
      //   ];
      //   if (alarmTime) {
      //     alarmTime = momont(alarmTime).format("YYYY-MM-DD HH:mm:ss");
      //   }
      //   this.$Notification({
      //     alarmId: id,
      //     type: "alarmed",
      //     alarmType: "取消温度告警",
      //     siteName,
      //     siteNo,
      //     alarmTime,
      //     aisleNo: channel,
      //     workNo,
      //   });
      // }
      // if (redata["网络告警"]) {
      //   let { id, siteNo, siteName, alarmTime, workNo } = redata["网络告警"];
      //   if (alarmTime) {
      //     alarmTime = momont(alarmTime).format("YYYY-MM-DD HH:mm:ss");
      //   }
      //   this.$Notification({
      //     alarmId: id,
      //     type: "alarming",
      //     alarmType: "网络告警",
      //     siteName,
      //     siteNo,
      //     alarmTime,
      //     workNo,
      //   });
      // }
      // if (redata["网络告警关闭"]) {
      //   let { id, siteNo, siteName, alarmTime, workNo } = redata[
      //     "网络告警关闭"
      //   ];
      //   if (alarmTime) {
      //     alarmTime = momont(alarmTime).format("YYYY-MM-DD HH:mm:ss");
      //   }
      //   this.$Notification({
      //     alarmId: id,
      //     type: "alarmed",
      //     alarmType: "取消网络告警",
      //     siteName,
      //     siteNo,
      //     alarmTime,
      //     workNo,
      //   });
      // }
      // if (redata["box告警"]) {
      //   let { id, siteNo, siteName, alarmTime, workNo } = redata["box告警"];
      //   if (alarmTime) {
      //     alarmTime = momont(alarmTime).format("YYYY-MM-DD HH:mm:ss");
      //   }
      //   this.$Notification({
      //     alarmId: id,
      //     type: "alarming",
      //     alarmType: "box告警",
      //     siteName,
      //     siteNo,
      //     alarmTime,
      //     workNo,
      //   });
      // }
      // if (redata["box告警关闭"]) {
      //   let { id, siteNo, siteName, alarmTime, workNo } = redata["box告警关闭"];
      //   if (alarmTime) {
      //     alarmTime = momont(alarmTime).format("YYYY-MM-DD HH:mm:ss");
      //   }
      //   this.$Notification({
      //     alarmId: id,
      //     type: "alarmed",
      //     alarmType: "取消box告警",
      //     siteName,
      //     siteNo,
      //     alarmTime,
      //     workNo,
      //   });
      // }
      if (redata["电池仓通道升温告警"]&&redata["电池仓通道升温告警"].category=="升温告警") {
        let { id, siteNo, siteName, alarmTime,channel, workNo,aisleHeatUp,warehouseHeatUp,heatUpDiffer} = redata["电池仓通道升温告警"];
        if (alarmTime) {
          alarmTime = momont(alarmTime).format("YYYY-MM-DD HH:mm:ss");
        }
        //不同于Notification 通知、Message 消息提示的一种自定义信息提示组件$tip
        this.$tip({
          alarmId: id,
          type: "warning",
          alarmType: "电池仓通道升温告警",
          siteName,
          channel,
          siteNo,
          aisleHeatUp,
          warehouseHeatUp,
          heatUpDiffer,
          alarmTime,
          workNo,
        });
      }
      if (redata["电池仓通道温度告警"]) {
        let { id, siteNo, siteName, alarmTime,channel, workNo, warehouseAvg,warehouseLevel,differ,lastTemp,} = redata["电池仓通道温度告警"];
        if (alarmTime) {
          alarmTime = momont(alarmTime).format("YYYY-MM-DD HH:mm:ss");
        }
        this.$tip({
          alarmId: id,
          type: "warning",
          alarmType: "电池仓通道定温告警",
          siteName,
          channel,
          siteNo,
          warehouseAvg,
          warehouseLevel,
          differ,
          lastTemp,
          alarmTime,
          workNo,
        });
      }
      if (redata["电池仓通道取消温度告警"]) {
        let { id, siteNo, siteName, alarmTime,channel, workNo, warehouseAvg,warehouseLevel,differ,lastTemp, } = redata["电池仓通道取消温度告警"];
        if (alarmTime) {
          alarmTime = momont(alarmTime).format("YYYY-MM-DD HH:mm:ss");
        }
        this.$tip({
          alarmId: id,
          type: "closed",
          alarmType: "电池仓通道取消温度告警",
          siteName,
          siteNo,
          channel,
          warehouseAvg,
          warehouseLevel,
          differ,
          lastTemp,
          alarmTime,
          workNo,
        });
      }
    },
  },
};
</script>

<style scoped>
.tttt {
  background-size: 100% 100%;
}
</style>